package org.lee.basic;

import java.math.BigDecimal;

/**
 * @author jackielee
 * @version 1.0
 * @description StringDemo
 * @date 2021/4/19
 */
public class StringDemo {

  public static void main(String[] args) {
    String s1 = "通话";
    String s2 = "重地";
    System.out.println(s1.hashCode() + ":" + s2.hashCode()); // 1179395:1179395
    System.out.println(s1 == s2); // false
    System.out.println(s1.equals(s2)); // false

    System.out.println(Math.round(-1.6)); // -2
    System.out.println(Math.round(-1.5)); // 中间值0.5向右取整：-1

    // byte,char,boolean,short,int,float,long,double基本类型
    // 多线程下使用StringBuffer，而其他情况下使用StringBuilder
    //    StringBuilder sb = new StringBuilder();sb.reverse(); // 反转
    //    TreeMap t = new TreeMap();
    //    t.put(null, null); // 安全 不可null key
    //    HashMap h = new HashMap();
    //    h.put(null, null); // 非安全 key 可为null
    //    Hashtable t1 = new Hashtable();
    //    t1.put(null, null); // 安全 key不为null
    //    ConcurrentHashMap cm; cm.put()
    s1.getBytes();
    s1.charAt(0);
    //    ConcurrentMap<String,String> m = new ConcurrentHashMap<>();m.put()
    //    LinkedBlockingDeque<String> q;
    //    q.push();
    Cat.sayHi();

    //    Files.exists("")
    //        Files
    char a = 'a';
    int iA = a;
    double dA = a;
    short sA = (short) a;
    byte bA = (byte) a;
    String stA = new String(new char[] {a});
    System.out.println("a:" + a + ",ia:" + iA + ",da:" + dA + ",ba:" + bA + ",strA:" + stA);

    int i1 = 1;
    Integer i2 = i1;
    int i3 = i2;
    //    Character,Boolean,Byte,Short,Integer,Long,Float,Double
    // 属性，方法，构造方法，内部类，代码块
    if (1.01 + 4 == 5) {
      System.out.println("5");
    }

    BigDecimal d = BigDecimal.valueOf(100.509);
    System.out.println(d);

    int i = 0;
    int ip1 = ++i; // 1
    int ip2 = i++; // 1
    System.out.println(ip1 + ":" + ip2);
    //    Object s; s.equals(),s.hashCode(),s.notify();,s.wait();s.getClass(),s.clone(),s.toString()

    {
      System.out.println("good");
    }

    Inner.ok();
    Inner.ok();

    //    AbstractStringBuilder
    //    StringBuilder
    //    StringBuffer

    //    s1.getBytes(Charset.forName("Utf-8"));

    System.out.println("=================================>>");
    String str1 = "hello";
    String str2 = new String("hello");
    System.out.println(str1 == str2); // false

    String c = "world";
    System.out.println(c.intern() == c); // true

    String str3 = new String("mike");
    System.out.println(str3.intern() == str3); // false

    //    intern() 会先查找stringtable，查到了就不会添加
    String str4 = new String("jo") + new String("hn");
    System.out.println(str4.intern() == str4); // true ->由于john字符串不在字符串常量表中，所以放进去后与当前相同

    String str5 = new String("ja") + new String("va");
    System.out.println(str5.intern() == str5); // false => 由于java字符串早已在字符串常量表中，所以与当前不同

    String str6 = new String("Str") + new String("ing");
    System.out.println(str5.intern() == str6); // false->由于String字符串早已在字符串常量表中，所以与当前不同

    System.out.println("************* Integer*****");
    Integer int1 = 153;
    Integer int2 = 153;
    //    System.identityHashCode(obj); // 获取对象的地址

    System.out.println(
        System.identityHashCode(int1)
            + "="
            + System.identityHashCode(int2)
            + ":int1==int2?"
            + (int1 == int2)); // 由于装箱、拆箱两个对象不同
    int int3 = int1;
    int int4 = int2; // 拆箱
    System.out.println(int3 == int4); // true 拆完是同一常量地址
    //    System.gc(); // 回收

  }

  // static：内部类，方法，变量，代码块
  static class Inner {
    static {
      System.out.println("static inner.");
    }

    static {
      System.out.println("static inner2");
    }

    static boolean ok() {
      System.out.println("ok now.");
      return true;
    }
  }
}
